import CryptoJS from 'crypto-js';

const keySalt = CryptoJS.enc.Utf8.parse('wethink202206280');

const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
 * 加密
 * @param word
 * @param key
 * @returns {string}
 */
export const encryptFun = (word, key) => {
    const srcs = CryptoJS.enc.Utf8.parse(word);
    const encrypted = CryptoJS.AES.encrypt(srcs, key == null ? keySalt : CryptoJS.enc.Utf8.parse(key), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
};

export const decryptFun = (word) => {
    const decrypt = CryptoJS.AES.decrypt(word, keySalt, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
};

export const randomString = (length) => {
    let result = '';
    for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    return result;
};
